package com.fingard.dsp.bank.directbank.cmb04;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingleBat;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingleBat;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CMB04SinglePay extends CMB04Base implements ISinglePay {
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = d_190802(p_req1908);
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
	
	private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail retDetail = new RetAtsSglDetail(p_req1908);

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "");
			if (actItem == null) {
				retDetail.respCode = "0";
				retDetail.respInfo = "未找到账号[" + p_req1908.singleDetail.payAct + "]商户配置信息!";
				retDetail.transState = PaymentState.FAIL_Payment;
				retDetail.payInfoCode = FailCode.E9981;
				return retDetail;
			}
			
			CMB04Header bankHeader = new CMB04Header();
			bankHeader.transCode = CMB04TransCode.deal_1908_190802;
			bankHeader.reqSeqID = getQryTransID();

			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter);

			XElement tmpXElement = new XElement("Request", "");
			tmpXElement.addChild(new XElement("merTrnSerial", p_req1908.singleDetail.askNumber));
			tmpXElement.addChild(new XElement("merTrnDt", Format.Date8Format.format(p_req1908.singleDetail.getPayDateTime())));
			tmpXElement.addChild(new XElement("merTrnTm", Format.Time6Format.format(p_req1908.singleDetail.getPayDateTime())));
			tmpXElement.addChild(new XElement("payAccNo", p_req1908.singleDetail.payAct));
			tmpXElement.addChild(new XElement("payAccName", p_req1908.singleDetail.payName));
			tmpXElement.addChild(new XElement("rcvAccNo", p_req1908.singleDetail.recAct));
			tmpXElement.addChild(new XElement("rcvAccName", p_req1908.singleDetail.recName));
			tmpXElement.addChild(new XElement("rcvBankName",p_req1908.singleDetail.recBankName));
			tmpXElement.addChild(new XElement("rcvBankAbbr",""));
			tmpXElement.addChild(new XElement("rcvBranchName", p_req1908.singleDetail.cnapsName));
			tmpXElement.addChild(new XElement("rcvBranchNo", p_req1908.singleDetail.cnapsCode));
			tmpXElement.addChild(new XElement("rcvPlace",p_req1908.singleDetail.recAreaName.replace("_", "")));
			
			String isPrivate = p_req1908.singleDetail.isPrivate;
			if ("1".equals(isPrivate)) {
				isPrivate = "P";
			} else {
				isPrivate = "C";
			}
			tmpXElement.addChild(new XElement("rcvAccType",isPrivate));
			tmpXElement.addChild(new XElement("trnTxt",p_req1908.singleDetail.memo));
			tmpXElement.addChild(new XElement("trnAmt", p_req1908.singleDetail.payAmount));
			tmpXElement.addChild(new XElement("trnChannel",""));
			tmpXElement.addChild(new XElement("reqNotify",""));
			tmpXElement.addChild(new XElement("reqMak",""));
			nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
			xmlWriter.writeXElement(tmpXElement);
			xmlWriter.writeEndDocument();
			
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader, actItem);

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
                String tempstr = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<?xml"),tmpStrRet[1].indexOf("</Response>")+11);
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tempstr, tmpSbMsg);
				if (tmpBkReader != null) {
					retDetail.respCode = "0";
					retDetail.respInfo = tmpBkReader.getFirstText("/Response/respMsg");
					retDetail.transState = tmpBkReader.getFirstText("/Response/respCode");
					
					String payInfoCode = tmpBkReader.getFirstText("/Response/respCode");
					if ("CMBDF01".equals(payInfoCode)) {
						payInfoCode = tmpBkReader.getFirstText("/Response/respMsg");
					}
					retDetail.payInfoCode = payInfoCode;
				} else {
					retDetail.respCode = "-1";
					retDetail.respInfo = tmpSbMsg.toString();
				}
			} else if (tmpStrRet[0].equals(ConnState.FAIL)) {
				retDetail.respCode = "0";
				retDetail.respInfo = tmpStrRet[1];
				retDetail.transState = PaymentState.FAIL_Payment;
				retDetail.payInfoCode = tmpStrRet[0];
			} else {
				retDetail.respCode = "-1";
				retDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retDetail.respCode = "-1";
			retDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retDetail;
	}

	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetAtsDetail = new RetAtsSglDetail(p_req1944);

		try {
			ActSetItem actItem = getBankConfig().getActSetItem(p_req1944.payAct, "");
			if (actItem == null) {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = "未找到账号" + p_req1944.payAct + "的商户配置节点";
				retAts.writeRetDetail(tmpRetAtsDetail);
				return retAts;
			}
			
			CMB04Header bankHeader = new CMB04Header();
			bankHeader.transCode = CMB04TransCode.deal_1944_190802;
			bankHeader.reqSeqID = getQryTransID();

			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter);

			xmlWriter.writeStartElement("Request");
			xmlWriter.writeElementString("merTrnSerial", p_req1944.askNumber);
			xmlWriter.writeElementString("merTrnDt", Format.Date8Format.format(p_req1944.getBgnDateTime()));
			xmlWriter.writeElementString("merTrnTm", Format.Time6Format.format(p_req1944.getBgnDateTime()));
			xmlWriter.writeElementString("trnAmt", "");
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader, actItem);

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
                String tempstr = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<?xml"),tmpStrRet[1].indexOf("</Response>")+11);
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tempstr, tmpSbMsg);
				if (tmpBkReader != null) {
					tmpRetAtsDetail.respCode = "0";
					tmpRetAtsDetail.respInfo = tmpBkReader.getFirstText("/Response/respMsg");
					tmpRetAtsDetail.transState = tmpBkReader.getFirstText("/Response/respCode");
					
					String payInfoCode = tmpBkReader.getFirstText("/Response/respCode");
					if ("CMBMB95".equals(payInfoCode)) {
						canBeNotExist(p_req1944, tmpRetAtsDetail);
					}
					if ("CMBDF01".equals(payInfoCode)) {
						payInfoCode = tmpBkReader.getFirstText("/Response/respMsg");
					}
					tmpRetAtsDetail.payInfoCode = payInfoCode;
					tmpRetAtsDetail.transAmt = tmpBkReader.getFirstText("/Response/trnAmt");
				} else {
					tmpRetAtsDetail.respCode = "-1";
					tmpRetAtsDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetAtsDetail.respCode = "-1";
				tmpRetAtsDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetAtsDetail.respCode = "-1";
			tmpRetAtsDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		retAts.writeRetDetail(tmpRetAtsDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}